package com.example.teaknow.Util;

import android.content.Context;


import com.example.teaknow.Bean.NewsBean;
import com.example.teaknow.Bean.TeaBean;
import com.example.teaknow.Dao.NewsDao;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @Author: Stars
 */
public class DataUtil {
    private static boolean flag1=false;
    private static boolean flag2=false;
    private static final String[] TeaType={"绿茶","青茶","黄茶","红茶","花茶"};
    private static final String[] TeaImageUrl={"http://www.chawh.net/uploads/20111006092602_8739.jpg",
            "http://www.chawh.net/uploads/sl_20130206133518_6408.jpg",
            "http://www.chawh.net/uploads/sl_20121208152111_3602.jpg",
            "http://www.chawh.net/uploads/20111008160813_5825.jpg",
            "http://www.chawh.net/uploads/sl_20120728104502_5860.jpg"};
    public static void getNewsData(Context context) throws IOException {
        if(flag1){

        }
        else {
            flag1=true;
            NewsDao newsDao=new NewsDao(context);
            String Url="http://www.17chaye.cn/cwh/list_89_23.html";
            Document document= Jsoup.connect(Url).get();
            Elements newsLists=document.select("div.content").select("article");

            for (int i=1;i<newsLists.size();i++){
                String url="http://www.17chaye.cn"+newsLists.get(i).select("a").attr("href");
                String imageUrl="http://www.17chaye.cn"+newsLists.get(i).select("a").select("img").attr("src");
                String title=newsLists.get(i).select("a").select("img").attr("alt");
                String time=newsLists.get(i).select("p").select("time").select("font").text();
                NewsBean newsBean=new NewsBean(title,time,url,imageUrl);
                newsBean.setType("头条");
                newsDao.insertNews(newsBean);
            }
        /*
        咨询
         */
            newsDao=new NewsDao(context);
            Url="http://www.17chaye.cn/ask/list_3.html";
            document= Jsoup.connect(Url).get();
            newsLists=document.select("div.content").select("article");

            for (int i=1;i<newsLists.size();i++){
                String url="http://www.17chaye.cn"+newsLists.get(i).select("a").attr("href");
                String imageUrl="http://www.17chaye.cn"+newsLists.get(i).select("a").select("img").attr("src");
                String title=newsLists.get(i).select("a").select("img").attr("alt");
                String time=newsLists.get(i).select("p").select("time").select("font").text();
                NewsBean newsBean=new NewsBean(title,time,url,imageUrl);
                newsBean.setType("咨询");
                newsDao.insertNews(newsBean);
            }
        /*
        经营
         */
            newsDao=new NewsDao(context);
            Url="http://www.17chaye.cn/pp/";
            document= Jsoup.connect(Url).get();
            newsLists=document.select("div.content").select("article");

            for (int i=1;i<newsLists.size();i++){
                String url="http://www.17chaye.cn"+newsLists.get(i).select("a").attr("href");
                String imageUrl="http://www.17chaye.cn"+newsLists.get(i).select("a").select("img").attr("src");
                String title=newsLists.get(i).select("a").select("img").attr("alt");
                String time=newsLists.get(i).select("p").select("time").select("font").text();
                NewsBean newsBean=new NewsBean(title,time,url,imageUrl);
                newsBean.setType("经营");
                newsDao.insertNews(newsBean);
            }
        }
        /*
        头条
         */


    }
    public static ArrayList<TeaBean> initTeaType(){
        ArrayList<TeaBean> teaBeans=new ArrayList<TeaBean>();
        for (int i=0;i<TeaType.length;i++){
            TeaBean teaBean=new TeaBean(TeaType[i],TeaImageUrl[i]);
            teaBeans.add(teaBean);
        }
        return teaBeans;
    }

    public static void getTeaData(String url,Context context) throws IOException {
        if(flag2){

        }
        /*
        绿茶
         */
        else {
            String Url="http://www.chawh.net/lc/";
            Document document= Jsoup.connect(Url).get();
            Elements TeaLists=document.select("div.pd2column");
            NewsDao newsDao=new NewsDao(context);
            for(Element Tea:TeaLists){
                String TeaUrl="http://www.chawh.net"+Tea.select("dl").select("dd").select("strong").select("a").attr("href");
                String title=Tea.select("dl").select("dd").select("strong").select("a").text();
                String time=Tea.select("div.title").select("h2").select("a").text();
                String imageUrl="http://www.chawh.net"+Tea.select("dl").select("dt").select("a").select("img").attr("src");
                NewsBean newsBean=new NewsBean(title,time,TeaUrl,imageUrl);
                newsBean.setType("绿茶");
                newsDao.insertNews(newsBean);
            }
        /*
        青茶
         */
            Url="http://www.chawh.net/qc/";
            document= Jsoup.connect(Url).get();
            TeaLists=document.select("div.pd2column");
            newsDao=new NewsDao(context);
            for(Element Tea:TeaLists){
                String TeaUrl="http://www.chawh.net"+Tea.select("dl").select("dd").select("strong").select("a").attr("href");
                String title=Tea.select("dl").select("dd").select("strong").select("a").text();
                String time=Tea.select("div.title").select("h2").select("a").text();
                String imageUrl="http://www.chawh.net"+Tea.select("dl").select("dt").select("a").select("img").attr("src");
                NewsBean newsBean=new NewsBean(title,time,TeaUrl,imageUrl);
                newsBean.setType("青茶");
                newsDao.insertNews(newsBean);
            }
        /*
        黄茶
         */
            Url="http://www.chawh.net/hc/";
            document= Jsoup.connect(Url).get();
            TeaLists=document.select("div.pd2column");
            newsDao=new NewsDao(context);
            for(Element Tea:TeaLists){
                String TeaUrl="http://www.chawh.net"+Tea.select("dl").select("dd").select("strong").select("a").attr("href");
                String title=Tea.select("dl").select("dd").select("strong").select("a").text();
                String time=Tea.select("div.title").select("h2").select("a").text();
                String imageUrl="http://www.chawh.net"+Tea.select("dl").select("dt").select("a").select("img").attr("src");
                NewsBean newsBean=new NewsBean(title,time,TeaUrl,imageUrl);
                newsBean.setType("黄茶");
                newsDao.insertNews(newsBean);
            }
        /*
        红茶
         */
            Url="http://www.chawh.net/hoc/";
            document= Jsoup.connect(Url).get();
            TeaLists=document.select("div.pd2column");
            newsDao=new NewsDao(context);
            for(Element Tea:TeaLists){
                String TeaUrl="http://www.chawh.net"+Tea.select("dl").select("dd").select("strong").select("a").attr("href");
                String title=Tea.select("dl").select("dd").select("strong").select("a").text();
                String time=Tea.select("div.title").select("h2").select("a").text();
                String imageUrl="http://www.chawh.net"+Tea.select("dl").select("dt").select("a").select("img").attr("src");
                NewsBean newsBean=new NewsBean(title,time,TeaUrl,imageUrl);
                newsBean.setType("红茶");
                newsDao.insertNews(newsBean);
            }
        /*
        花茶
         */
            Url="http://www.chawh.net/hca/";
            document= Jsoup.connect(Url).get();
            TeaLists=document.select("div.pd2column");
            newsDao=new NewsDao(context);
            for(Element Tea:TeaLists){
                String TeaUrl="http://www.chawh.net"+Tea.select("dl").select("dd").select("strong").select("a").attr("href");
                String title=Tea.select("dl").select("dd").select("strong").select("a").text();
                String time=Tea.select("div.title").select("h2").select("a").text();
                String imageUrl="http://www.chawh.net"+Tea.select("dl").select("dt").select("a").select("img").attr("src");
                NewsBean newsBean=new NewsBean(title,time,TeaUrl,imageUrl);
                newsBean.setType("花茶");
                newsDao.insertNews(newsBean);
            }
        }

    }

}
